/**
 * Created by Churcy on 2017/1/10.
 */
window.onload = function () {
    Company.searchCompany();
}

var Company = {

    searchCompany: function () {
        $("#searchBTN").on('click', function () {
            var conditions = getCurrentConditions();
            conditions.pageNo = 1;
            ajaxSearchCompanyList(conditions);
        });
        $("#resetBTN").on('click', function () {
            resetFn();
        });
    }
}
function searchFn() {
    var conditions = getCurrentConditions();
    console.log("searchFN")
    var response = ajaxSearchCompanyList(conditions);
}
function resetFn() {
    $("#comAddress").val("");
    $("#comName").val("");
}

function getCurrentConditions() {
    console.log("getCurrentConditions");
    var comAddress = $("#comAddress").val();
    var comName = $("#comName").val();
    var pageSize = $("#n").val();
    var pageNo = $("#p").val();
    var appKey = $("#appKey").val();

    var conditions = {
        comName: encodeURI(comName),
        comAddress: encodeURI(comAddress),
        pageIndex: pageNo,
        appKey: APP_KEY
    };
    console.log("输出查询条件: " + conditions.comName + "&" + conditions.comAddress);
    return conditions;

}
function ajaxSearchCompanyList(conditions) {
    console.log("ajaxSearchHospitalList");
    $.ajax({
        type: "GET",
        url: PROJECT_CTX + "/trafficback/companyList",
        data: conditions,
        dataType: "json",
        success: function (data) {
            console.log(data)
            var result = eval(data);
            var resultCode = result.status.code;
            console.log(resultCode)
            console.log(result.line);
            if (1 == resultCode) {
                // 清空当前数据
                var companyList = $("#companyList").empty();
                // 填写查询出来的数据
                $.each(result.line, function (i, item) {
                    var tr = "<tr>";

                    tr += "<td >";
                    tr += item.comAddress;
                    tr += "</td>"

                    tr += "<td >";
                    tr += item.comCode;
                    tr += "</td>"

                    tr += "<td >";
                    tr += item.comName;
                    tr += "</td>"

                    tr += "<td >";
                    tr += item.comParentCode;
                    tr += "</td>"

                    tr += "<td >";
                    tr += " <a title='修改' href='#'>修改</a>";
                    /*tr += " <a title='删除' href='#'>删除</a>"; */
                    tr += "</td >";

                    tr += "</tr>";
                    companyList.append(tr);
                    // 更新分页栏
                    reloadPage(result);
                });
            }

        }

        ,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest.status);
            console.log(XMLHttpRequest.readyState);
            console.log(textStatus);
        }
    });
}

function deleteCompany(comId, sftpId, accountId, appKey, comCode) {
    swal({
            title: "确定删除么?",
            text: "删除后您将无法恢复,如需恢复请联系管理员!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "确定, 删除!",
            closeOnConfirm: false,
            showLoaderOnConfirm: true,
        },
        function () {
            setTimeout(function () {
                var conditions = {
                    comId: comId,
                    sftpId: sftpId,
                    accountId: accountId
                }
                $.ajax({
                    type: "GET",
                    data: conditions,
                    url: PROJECT_CTX + "/traffic/company/delete",
                    dataType: "json",
                    success: function (data) {
                        alert(1)
                    }
                })
                swal({
                        title: "已删除",
                        text: "未录入设备信息已删除",
                        type: "success"
                    },
                    function () {
                        window.location.href = PROJECT_CTX + "/traffic/company?appKey=" + appKey + "&comCode=" + comCode;
                    });

            }, 2000);
        });

}